package cn.hquanheng.java_base.day15;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

public class TestRegex {
	@Test
	public void test() {


		String regex = ".\\.(pdf|docx)";
		System.out.println("a.pdf".matches(regex));
		System.out.println("a.pd".matches(regex));
		System.out.println("a.df".matches(regex));
		System.out.println("a.apdf".matches(regex));
		System.out.println("a.docx".matches(regex));
		}
	/*
	 * . 任意一个字符
	 * * 次数 0次以上
	 *  \ 转义字符		
	 */
	@Test 
	public void test1() {
		String regex = "^abc.*";//以abc 开头
	}
	@Test
	public void test2() {
		String regex = ".*abc$";//以abc 结尾		
	}
	@Test
	public void test3() {
		String regex1 = "ab*c";
		System.out.println("ac".matches(regex1));
		System.out.println("abc".matches(regex1));
		System.out.println("abbc".matches(regex1));
		System.out.println("abbbc".matches(regex1));
		System.out.println();
		
		String regex2 = "ab+c";
		System.out.println("ac".matches(regex2));
		System.out.println("abc".matches(regex2));
		System.out.println("abbc".matches(regex2));
		System.out.println("abbbc".matches(regex2));
		System.out.println();
		
		String regex3 = "ab?c";
		System.out.println("ac".matches(regex3));
		System.out.println("abc".matches(regex3));
		System.out.println("abbc".matches(regex3));
		System.out.println("abbbc".matches(regex3));
		System.out.println();
		
		String regex4 = "ab{2,}c";// B只能出现两次	{m,n}
		System.out.println("ac".matches(regex4));
		System.out.println("abc".matches(regex4));
		System.out.println("abbc".matches(regex4));
		System.out.println("abbbc".matches(regex4));
		
	}
	@Test
	public void test4() {
		String regex = "(ab){1,}";
		System.out.println("ababababab".matches(regex));
	}
	@Test
	public void test5() {
		String regex = ".*[abc].*";//字符串中包含abc其中一个
		System.out.println("edf".matches(regex));
		System.out.println("adf".matches(regex));
		System.out.println("bdf".matches(regex));
		System.out.println("123edf".matches(regex));
	}
	@Test
	public void test6() {
		//String regex = "[^abc]";//判断单个字符 不是abc其中一个
		String regex = "[^我们]+";//".*[^abc].*"//中间有一个不是abc
		System.out.println("我".matches(regex));
		System.out.println("b".matches(regex));
		System.out.println("c".matches(regex));
		System.out.println("d".matches(regex));
		System.out.println("edf".matches(regex));
		System.out.println("a我f".matches(regex));
		System.out.println("们df".matches(regex));
		System.out.println("123edf".matches(regex));
	}
	@Test
	public void test7() {
		String regex = "[a-zA-Z0-9]";
		System.out.println("f".matches(regex));
		System.out.println("G".matches(regex));
		System.out.println("8".matches(regex));
		System.out.println("*".matches(regex));
		System.out.println("fG8".matches(regex)); 
		System.out.println();
		regex = ".*[a-z].*";
		System.out.println("FGfg".matches(regex));
		System.out.println("F&G*f#g".matches(regex));
		System.out.println("23434#5".matches(regex));
		
	}
	@Test
	public void test8() {
		String s = "a	b c d   e";
		String replace = s.replaceAll("\\s","");
		System.out.println(replace);
	}
	@Test
	public void test9() {
		try {
			byte[] bytes = Files.readAllBytes(Paths.get("E:\\Eclipse\\winter_19\\src\\styles.css"));
			String s = new String(bytes);
			String replace = s.replaceAll("\\s","");
			System.out.println(replace);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test10() {
		String regex = ".{3,5}";
		System.out.println("1b#D".matches(regex));
		
		regex = ".*\\d+.*";
		System.out.println("ab1".matches(regex));
		System.out.println("1bc".matches(regex));
		System.out.println("ab1EF6".matches(regex));
		
		System.out.println();
		regex = "(?=.*\\d.*).{3,5}";
		System.out.println("ab1".matches(regex));
		System.out.println("abc".matches(regex));
		System.out.println("321".matches(regex));
		System.out.println("1bc".matches(regex));
		System.out.println("ab1EF6".matches(regex));
		System.out.println();
		
		regex = "(?=.{3,5}).*\\d.*";
		System.out.println("ab1".matches(regex));
		System.out.println("abc".matches(regex));
		System.out.println("321".matches(regex));
		System.out.println("1bc".matches(regex));
		System.out.println("ab1EF6".matches(regex));
		
	}
@Test
	public void test11() {
	String regex ="\\d+";//全部是数字（"\\d+"）
	regex = "[1-9]\\d+";
	regex = "(\\d|([1-9]\\d+))";
	System.out.println("0".matches(regex));
	System.out.println("10".matches(regex));
	System.out.println("01".matches(regex));
	System.out.println("-1".matches(regex));
	
}
	public void test12(){
		String regex = "[1-9][0-9]\\d";
		System.out.println("0".matches(regex));
		System.out.println("5".matches(regex));
		System.out.println("01".matches(regex));
		System.out.println("10".matches(regex));
		System.out.println("90".matches(regex));
		
	}
	public void test13() {
		String regex = "[12]\\d\\d";
		System.out.println("1".matches(regex));
		System.out.println("11".matches(regex));
		System.out.println("100".matches(regex));
		System.out.println("199".matches(regex));
		System.out.println("200".matches(regex));
		System.out.println("255".matches(regex));
	}
	
}
